"""
@author: wy
@file: 14.py
@time: 2023/12/7 19:07
"""
#14、使用python实现5行5列的盘龙数组
#[1, 2, 3, 4, 5]
#[16, 17, 18, 19, 6]
#[15, 24, 25, 20, 7]
#[14, 23, 22, 21, 8]
#[13, 12, 11, 10, 9]
#坚持循环不变量原则 边界条件
#[1,2,3]
#[8,9,4]
#[7,6,5]
#[1,  2,  3,  4]
#[12, 13, 14, 5]
#[11, 16, 15, 6]
#[10, 9,  8,  7]

def panlong(n):
    list1 = [[0]*n for _ in range(n)] #初始化数组
    startx=0
    starty=0
    mid = n//2
    loop = n // 2
    offset=1
    x=1
    while (loop):
        #行不变为startx 列从starty到 n-offset 左—>右
        for i in range(starty,n-offset):
            list1[startx][i]=x
            x += 1
        #列不变为n-offset 行从startx到n-offset  上->下
        for i in range(startx,n-offset):
            list1[i][n-offset]=x
            x += 1
        #行不变为n-offset  列从n-offset到 starty  右->左
        for i in range(n-offset,starty,-1):
            list1[n - offset][i] = x
            x += 1
        #列不变为starty   行从n-offset到startx   下->上
        for i in range(n-offset,startx,-1):
            list1[i][starty]  = x
            x += 1

        offset += 1
        startx += 1
        starty += 1
        loop -= 1

    if (n%2):
        list1[mid][mid]=n*n

    print(list1)


panlong(6)
